package com.seuic.scannerapitest.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.scandemo.R;
import com.seuic.misc.Misc;
import com.seuic.touch.TouchService;

public class MainActivity extends Activity implements OnClickListener{
	private EditText mEditText;
	private Button bt_test,bt_1,bt_2,bt_3,bt_4,bt_5,bt_6;
	private final static String PATH = Environment.getExternalStorageDirectory()+"/zipFile.zip";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mEditText = (EditText) findViewById(R.id.text);
		bt_test = (Button) findViewById(R.id.bt_test);
		bt_test.setOnClickListener(this);
		
		bt_1 = (Button) findViewById(R.id.bt_1);
		bt_1.setOnClickListener(this);
		
		bt_2 = (Button) findViewById(R.id.bt_2);
		bt_2.setOnClickListener(this);
		
		bt_3 = (Button) findViewById(R.id.bt_3);
		bt_3.setOnClickListener(this);
		
		bt_4 = (Button) findViewById(R.id.bt_4);
		bt_4.setOnClickListener(this);
		
		bt_5 = (Button) findViewById(R.id.bt_5);
		bt_5.setOnClickListener(this);
		
		bt_6 = (Button) findViewById(R.id.bt_6);
		bt_6.setOnClickListener(this);
	}

	@Override
	protected void onDestroy() {
		stopScan();
		InnerScanner.getInstance(getApplicationContext()).close();
		super.onDestroy();
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() != KeyEvent.ACTION_UP) {
			onKeyScan();
		}

		return super.dispatchKeyEvent(event);
	}

	/**
	 * 扫码入口：按扫码按键的时候调用这个方法
	 */
	public void onKeyScan() {
		scan();
	}

	/**
	 * 
	 * @param onScanListener
	 * @param timeout
	 */
	private void scan() {
		InnerScanner.getInstance(getApplicationContext()).setOnScanListener(
				new OnScanListener() {
					@Override
					public void onScanSuccess(String arg0) {
						mEditText.setText("onScanSuccess " + arg0);
						stopScan();
					}

					@Override
					public void onScanFail(int arg0) {
						mEditText.setText("onScanFail " + arg0);
						stopScan();
					}

					@Override
					public void onCrash(String err) {
						mEditText.setText("onCrash " + err);
						stopScan();
					}
				});
		InnerScanner.getInstance(getApplicationContext()).start(10000);
	}

	public void stopScan() {
		InnerScanner.getInstance(getApplicationContext()).stopListen();
		InnerScanner.getInstance(getApplicationContext()).stop();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_test:
			new Thread(){
				public void run() {
					final Boolean b = InnerScanner.getInstance(getApplicationContext()).silentInstall(PATH);
					
					 new Handler(Looper.getMainLooper()).post(new Runnable() {
			             @Override
			             public void run() {
			            	Toast.makeText(MainActivity.this, "安装结果："+b, Toast.LENGTH_SHORT).show(); 
			             }
			         });
				}
			}.start();
			break;

		case R.id.bt_1:
			InnerScanner.getInstance(getApplicationContext()).setHomeDisable();
			break;
		case R.id.bt_2:
			InnerScanner.getInstance(getApplicationContext()).setBackDisable();
			break;
		case R.id.bt_3:
			InnerScanner.getInstance(getApplicationContext()).setMenuDisable();
			break;
		case R.id.bt_4:
			InnerScanner.getInstance(getApplicationContext()).setVirKeyDisabled(0);
			break;
		case R.id.bt_5:
			InnerScanner.getInstance(getApplicationContext()).setStatusBarEnabled(Misc.STATUSBAR_DISABLED);
			break;
		case R.id.bt_6:
			InnerScanner.getInstance(getApplicationContext()).setStatusBarEnabled(Misc.STATUSBAR_ENABLED);
			break;
		}
		
	}
}
